import Vue from 'vue'
import {Connect} from './config'

/**
 * @description 控制台输出请求结果
 * @param {请求接口} link
 * @param {返回参数} re
 */
let showResult = (link, re) => {
    console.log(`接口${link}请求成功，返回结果:${re}`)
}

/**
 * @description 微信post fromdata请求
 * @date 2018-11-24
 * @param {请求参数} params
 * @returns 响应结果
 */
function wxRequest_postForm(params) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${Connect.baseUrl}${params.url}`,
            data: params.data,
            header: {'content-type': 'application/x-www-form-urlencoded'},
            method: 'post',
            success: res => {
                showResult(params.url, res.data)
                resolve(res.data)
            },
            fail: e => {
                reject(res)
            },
        })
    })
}

/**
 * @description 微信post json请求
 * @date 2018-11-24
 * @param {请求参数} params
 * @returns 响应结果
 */
function wxRequest_postJSON(params) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${Connect.baseUrl}${params.url}`,
            data: params.data,
            header: {'content-type': 'application/json'},
            method: 'post',
            success: res => {
                showResult(params.url, res.data)
                resolve(res.data)
            },
            fail: e => {
                reject(res)
            },
        })
    })
}

const wxr = {
    post_form: wxRequest_postForm,
    post_json: wxRequest_postJSON,
}

Vue.prototype.$wxr = wxr
